
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
using WooEngine.Extensions;
using UnityEngine.EventSystems;
#endif
namespace WooEngine.UI.Base
{
    public class UIButtonFS : MonoBehaviour
    {
        public Button Btn;
        public Image Img;
        public Text Txt;
        public Text Des;
        public Sprite HasImg;
        public Sprite LackImg;

        public GameObject[] HasShowGO;
        public GameObject[] LackShowGO;

#if UNITY_EDITOR
        [MenuItem("GameObject/UI/UIButtonFS", false, 3001)]
        private static void OnCreateUIComponent()
        {
            var view = new GameObject("UIButtonFS").GetOrAddComponent<RectTransform>();
            view.gameObject.layer = LayerMask.NameToLayer("UI");
#if UNITY_2021_2_OR_NEWER
            if (UnityEditor.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage() != null)
#else
            if (UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage() != null)
#endif
            {
                if (Selection.activeGameObject != null)
                    view.SetParent(Selection.activeGameObject.transform, false);
            }
            else
            {
                var canvas = FindObjectOfType<Canvas>();
                if (canvas == null)
                {
                    canvas = new GameObject("Canvas").AddComponent<Canvas>();
                    canvas.renderMode = RenderMode.ScreenSpaceOverlay;

                    canvas.GetOrAddComponent<CanvasScaler>();
                    canvas.GetOrAddComponent<GraphicRaycaster>();
                }

                var eventsystem = FindObjectOfType<EventSystem>();
                if (eventsystem == null)
                {
                    eventsystem = new GameObject("EventSystem").AddComponent<EventSystem>();
                    eventsystem.GetOrAddComponent<StandaloneInputModule>();
                }

                GameObject go = Selection.activeGameObject;
                if (go != null && go.TryGetComponent<RectTransform>(out RectTransform rectTransform))
                {
                    view.SetParent(go.transform, false);
                }
                else
                {
                    view.SetParent(canvas.transform, false);
                }
            }



            var comp = view.GetOrAddComponent<UIButtonFS>();
            comp.Btn = new GameObject("Button").GetOrAddComponent<Button>();
            comp.Btn.transform.SetParent(view);
            comp.Btn.transform.localScale = Vector3.one;
            comp.Btn.transform.localPosition = Vector3.zero;
            comp.Btn.image = comp.Btn.GetOrAddComponent<Image>();
            comp.Btn.gameObject.layer = LayerMask.NameToLayer("UI");

            comp.Img = new GameObject("Image").GetOrAddComponent<Image>();
            comp.Img.transform.SetParent(view);
            comp.Img.transform.localScale = Vector3.one;
            comp.Img.transform.localPosition = Vector3.zero;
            comp.Img.gameObject.layer = LayerMask.NameToLayer("UI");
            comp.Img.raycastTarget = false;

            comp.Txt = new GameObject("Text").GetOrAddComponent<Text>();
            comp.Txt.transform.SetParent(view);
            comp.Txt.transform.localScale = Vector3.one;
            comp.Txt.transform.localPosition = Vector3.zero;
            comp.Txt.gameObject.layer = LayerMask.NameToLayer("UI");
            comp.Txt.raycastTarget = false;

            comp.Des = new GameObject("Des").GetOrAddComponent<Text>();
            comp.Des.transform.SetParent(view);
            comp.Des.transform.localScale = Vector3.one;
            comp.Des.transform.localPosition = Vector3.zero;
            comp.Des.gameObject.layer = LayerMask.NameToLayer("UI");
            comp.Des.raycastTarget = false;
        }
#endif

        public void SwitchBtn(bool isHas)
        {
            Img.gameObject.SetActive(false);
            Txt.gameObject.SetActive(false);
            Des.gameObject.SetActive(false);
            if (isHas)
            {
                Btn.image.sprite = HasImg;
                for(int i = 0; i < HasShowGO.Length; i++)
                {
                    HasShowGO[i].SetActive(true);
                }
            }
            else
            {
                Btn.image.sprite = LackImg;
                for (int i = 0; i < LackShowGO.Length; i++)
                {
                    LackShowGO[i].SetActive(true);
                }
            }

            if (Img.gameObject.activeSelf)
            {
                Txt.alignment = TextAnchor.MiddleLeft;
                Txt.transform.localPosition = new Vector3(Img.GetComponent<RectTransform>().sizeDelta.x / 2 + Img.transform.localPosition.x + Txt.GetComponent<RectTransform>().sizeDelta.x / 2 + 5, 0, 0);
            }
            else {
                Txt.transform.localPosition = Vector3.zero;
                Txt.alignment = TextAnchor.MiddleCenter;
            }


            if (Btn.image.sprite != null)
            {
                Btn.image.color = Color.white;
            }
            else
            {
                Btn.image.color = new Color(0, 0, 0, 0);
            }
        }
    }

    
}